package com.ms.oschina.api.remote;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import com.ms.oschina.api.remote.OkHttpClientManager.OnGetNetResultListener;
import com.ms.oschina.bean.Active;
import com.ms.oschina.bean.ActiveList;
import com.ms.oschina.bean.BlogDetail;
import com.ms.oschina.bean.BlogList;
import com.ms.oschina.bean.CommentList;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.Event;
import com.ms.oschina.bean.EventList;
import com.ms.oschina.bean.Favorite;
import com.ms.oschina.bean.FavoriteList;
import com.ms.oschina.bean.Friend;
import com.ms.oschina.bean.FriendsList;
import com.ms.oschina.bean.LoginUserBean;
import com.ms.oschina.bean.MyInformation;
import com.ms.oschina.bean.NewsDetail;
import com.ms.oschina.bean.NewsList;
import com.ms.oschina.bean.PostDetail;
import com.ms.oschina.bean.Result;
import com.ms.oschina.bean.TweetDetail;
import com.ms.oschina.bean.TweetsList;
import com.ms.oschina.task.LoginTask;
import com.ms.oschina.task.UploadTask;
import com.ms.oschina.util.LogUtils;
import com.ms.oschina.util.PreferenceUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.util.XmlUtils;

public class OsChinaApi {
	public static final int pageSize = Constants.PAGESIZE;

	/**
	 * 最新动弹
	 */
	public static final int NEWESTTWIST = TweetsList.CATALOG_LATEST;
	/**
	 * 热门动弹
	 */
	public static final int HOTSPOTTWIST = TweetsList.CATALOG_HOT;

	/**
	 * 我的动弹
	 */
	public static final int MYTTWIST = TweetsList.CATALOG_ME;

	/**
	 * 资讯
	 */
	public static final int CATEGORY_NEWSINFORMATION = 1;

	/**
	 * 热点
	 */
	public static final int CATEGORY_NEWSHOTSPOT = 4;

	/**
	 * 动弹评论
	 */
	public static final int CATEGORY_TWEETCOMMENT = 3;

	private static final String WEEK = "week";
	private static final String MONTH = "month";
	private static String currentShowHotspot = WEEK;

	public static final String URL_NEWSINFORMATION = "news_list?pageIndex=%s&catalog="
			+ CATEGORY_NEWSINFORMATION + "&pageSize=" + pageSize;

	public static final String URL_HOTSPOT = "news_list?pageIndex=%s&catalog="
			+ CATEGORY_NEWSHOTSPOT + "&pageSize=" + pageSize + "&show=%s";

	public static final String URL_Blog = "blog_list?pageIndex=%s&pageSize="
			+ pageSize + "&type=%s";

	public static final String URL_NEWSInfoDETAILS = "news_detail?id=%s";
	public static final String URL_NEWSBLOGDETAILS = "blog_detail?id=%s";

	public static final String URL_TWEET = "tweet_list?pageIndex=%s"
			+ "&pageSize=" + pageSize + "&uid=%s";

	public static final String URL_TWEET_DETAIL = "tweet_detail?id=%s";

	public static final String URL_TWEETCOMMENT = "comment_list?pageIndex=%s&catalog="
			+ CATEGORY_TWEETCOMMENT + "&pageSize=" + pageSize + "&id=%s";

	public static final String URL_LOGIN = "login_validate";

	public static final String URL_LOGIN_Cookie = "keep_login=1&username=%s&pwd=%s";
	//
	public static final String URL_LOGIN_Token = "my_information?uid=%s";

	public static final String URL_FAVORITE = "favorite_list?uid=%s&pageIndex=%s"
			+ "&pageSize=" + pageSize + "&type=%s";

	public static final String URL_FRIEND = "friends_list?uid=%s&pageIndex=%s"
			+ "&pageSize=" + pageSize + "&relation=%s";

	public static final String URL_AT = "active_list?uid=%s&pageIndex=%s"
			+ "&pageSize=" + pageSize + "&catalog=%s";

	public static final String URL_FRIENDS = "active_list?uid=%s&pageIndex=%s"
			+ "&pageSize=" + pageSize + "&catalog=%s";

	/**
	 * 活动
	 */
	public static final String URL_EVENT = "event_list?uid=%s&pageIndex=%s"
			+ "&pageSize=" + pageSize;

	/**
	 * 活动详情
	 */
	public static final String URL_EVENT_DETAIL = "post_detail?id=%s";
	/*
	 * 活动详情
	 */
	public static final String URL_TWEET_PUB = "tweet_pub";

	/**
	 * 博客
	 */
	public static final int CATEGORY_NEWSBLOG = 2;
	/**
	 * 资讯
	 */
	public static final int CATEGORY_NEWSRECOMMEND = 3;

	// String string = OkHttpClientManager.getInstance()._getAsString(
	// "news_list?pageIndex=0&catalog=1&pageSize=20");

	public static void getNewsList(int pageIndex, int catalog) {
		String url = String.format(URL_NEWSINFORMATION, pageIndex, pageSize);
		switch (catalog) {
		case CATEGORY_NEWSINFORMATION:
			// 拼装字符串
			// 请求数据
			// 返回结果
			// 解析xml
			// 返回集合
			break;
		case CATEGORY_NEWSHOTSPOT:
			break;
		case CATEGORY_NEWSBLOG:
			break;
		case CATEGORY_NEWSRECOMMEND:
			break;
		}

	}

	/**
	 * 返回新闻资讯 信息
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static NewsList getNewsInformation(int pageIndex) throws IOException {
		// 拼装字符串
		String url = String.format(URL_NEWSINFORMATION, pageIndex);
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(NewsList.class, xmlString.getBytes());
	}

	/**
	 * 返回博客 信息
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static BlogList getNewsBlog(int pageIndex) throws IOException {
		// 拼装字符串
		String url = String.format(URL_Blog, pageIndex, "latest");
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(BlogList.class, xmlString.getBytes());
	}

	/**
	 * 返回热点 信息
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static NewsList getNewsHotspot(int pageIndex) throws IOException {
		// 拼装字符串
		String url = String.format(URL_HOTSPOT, pageIndex, currentShowHotspot);
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(NewsList.class, xmlString.getBytes());
	}

	/**
	 * 返回新闻详情 信息
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static NewsDetail getNewsInfoDetails(int id) throws IOException {
		// 拼装字符串
		String url = String.format(URL_NEWSInfoDETAILS, id);
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(NewsDetail.class, xmlString.getBytes());
	}

	/**
	 * 返回博客详情 信息
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static BlogDetail getNewsBlogDetails(int id) throws IOException {
		// 拼装字符串
		String url = String.format(URL_NEWSBLOGDETAILS, id);
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(BlogDetail.class, xmlString.getBytes());
	}

	/**
	 * 返回热门动弹
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static TweetsList getNewesTwist(int uid, int pageIndex)
			throws IOException {
		// 拼装字符串
		String url = String.format(URL_TWEET, pageIndex, uid);
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(TweetsList.class, xmlString.getBytes());
	}

	public static TweetDetail getTweetDetail(int id) throws IOException {
		// 拼装字符串
		String url = String.format(URL_TWEET_DETAIL, id);
		// 请求数据
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);

		// 解析xml返回结果
		return XmlUtils.toBean(TweetDetail.class, xmlString.getBytes());
	}

	public static <T> void getAsyncTweetComment(int id, int pageIndex,
			int requestCode,
			OnGetNetResultListener<CommentList> onGetNetResultListener) {
		// 拼装字符串
		String url = String.format(URL_TWEETCOMMENT, pageIndex, id);
		// 请求数据
		OkHttpClientManager.getInstance().getAsyncAsString(url, requestCode,
				CommentList.class, onGetNetResultListener);
	}

	public static CommentList getSyncTweetComment(int id, int pageIndex)
			throws IOException {
		// 拼装字符串
		String url = String.format(URL_TWEETCOMMENT, pageIndex, id);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		// 解析xml返回结果
		return XmlUtils.toBean(CommentList.class, xmlString.getBytes());
	}

	/**
	 * @param user
	 * @param password
	 * @param listener
	 * @throws UnsupportedEncodingException
	 * 
	 */
	public static void login(String user, String password,
			OnGetNetResultListener<LoginUserBean> listener)
			throws UnsupportedEncodingException {
		user = URLEncoder.encode(user, "utf-8");
		// 请求登录
		LoginTask task = new LoginTask(URL_LOGIN, 0, user, password, listener);
		task.execute();

		// url http://www.oschina.net/action/api/login_validate post
		// 携带cookie： keep_login=1&username=%s&pwd=%s
		// username内容需要转义URLDecoder.decode(s,"utf-8");

		// 抓包获取uid
		// post 方式提交数据
		// login_validate
		// 得到uid 2823808 和cookie

		// url http://www.oschina.net/action/api/my_information?uid=%s 携带cookie

		// 通过uid 和cookie 获取账户信息
	}

	public static MyInformation loginUser(int id) throws Exception {
		String url = String.format(URL_LOGIN_Token, id);
		String info = OkHttpClientManager.getInstance().getSyncAsString(url);
		MyInformation myinfo = XmlUtils.toBean(MyInformation.class,
				info.getBytes());
		if (myinfo == null) {
			throw new NullPointerException();
		}
		return myinfo;
	}

	public static List<Favorite> getFavoriteInfo(int pageIndex, int type)
			throws IOException {
		// 拼装字符串
		int uid = PreferenceUtils.getPrefInt(UiUtils.getContext(),
				Constants.UID, -1);
		String url = String.format(URL_FAVORITE, uid, pageIndex, type);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		// 解析xml返回结果
		return XmlUtils.toBean(FavoriteList.class, xmlString.getBytes())
				.getList();
	}

	/**
	 * 得到好友信息 ： 关注和 粉丝
	 * 
	 * @param pageIndex
	 * @param type
	 * @return
	 * @throws IOException
	 */
	public static List<Friend> getFriendInfo(int pageIndex, int type)
			throws IOException {
		// 拼装字符串
		int uid = PreferenceUtils.getPrefInt(UiUtils.getContext(),
				Constants.UID, -1);
		String url = String.format(URL_FRIEND, uid, pageIndex, type);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		return XmlUtils.toBean(FriendsList.class, xmlString.getBytes())
				.getList();
	}

	/**
	 * 
	 * @param pageIndex
	 * @param type
	 *            CATALOG_LASTEST CATALOG_ATME CATALOG_COMMENT CATALOG_MYSELF
	 * @return
	 * @throws IOException
	 */
	public static List<Active> getAboutMeInfo(int pageIndex, int type)
			throws IOException {
		// 拼装字符串
		int uid = PreferenceUtils.getPrefInt(UiUtils.getContext(),
				Constants.UID, -1);
		String url = String.format(URL_AT, uid, pageIndex, type);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		return XmlUtils.toBean(ActiveList.class, xmlString.getBytes())
				.getList();
	}

	/**
	 * 返回好友圈信息
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static List<Active> getFriendsInfo(int pageIndex) throws IOException {
		// 拼装字符串
		int uid = PreferenceUtils.getPrefInt(UiUtils.getContext(),
				Constants.UID, -1);
		if (uid == -1) {
			return null;
		}

		String url = String.format(URL_FRIENDS, uid, pageIndex, 1);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		return XmlUtils.toBean(ActiveList.class, xmlString.getBytes())
				.getList();
	}

	/**
	 * 返回活动信息的列表
	 * 
	 * @param uid
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static List<Event> getActivitiesList(int pageIndex, int uid)
			throws IOException {
		// 拼装字符串
		String url = String.format(URL_EVENT, uid, pageIndex);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		return XmlUtils.toBean(EventList.class, xmlString.getBytes()).getList();
	}

	/**
	 * 活动详情
	 * 
	 * @param uid
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static PostDetail getActivitiesDetail(int id) throws IOException {
		String url = String.format(URL_EVENT_DETAIL, id);
		String xmlString = OkHttpClientManager.getInstance().getSyncAsString(url);
		return XmlUtils.toBean(PostDetail.class, xmlString.getBytes());
	}

	/**
	 * 活动详情
	 * 
	 * @param uid
	 * 
	 * @param uid
	 * @param message
	 * @param listener
	 * 
	 * @return
	 * 
	 * @throws IOException
	 */
	public static void UploadFile(String path, String uid, String message,
			OnGetNetResultListener<Result> listener) throws IOException {

		UploadTask task = new UploadTask(URL_TWEET_PUB, 0, uid, message, path,
				listener);
		task.execute();
	}
}
